package st.business.tepth.business.history.detail;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.business.bean.HistoryDetailBean;
import st.business.tepth.business.history.detail.entity.Lv_1;
import st.business.tepth.business.history.detail.entity.Lv_2;
import st.business.tepth.business.history.detail.entity.Lv_2_1;
import st.business.tepth.business.history.detail.entity.Lv_2_1_1;
import st.business.tepth.business.history.detail.entity.Lv_3;
import st.business.tepth.business.history.detail.entity.Lv_3_1;
import st.business.tepth.business.history.detail.entity.Lv_3_2;
import st.business.tepth.business.history.detail.entity.Lv_4;
import st.business.tepth.business.history.detail.entity.Lv_5;
import st.business.tepth.business.history.detail.entity.Lv_5_1;

/**
 * @author zhangyu
 * @date 2019/5/28
 */
public class HistoryDetailDataConvert {
    public List<MultiItemEntity> convert(HistoryDetailBean bean) {
        List<MultiItemEntity> entityList = new ArrayList<>();
        if (bean != null) {
            List<HistoryDetailBean.TaskContentInfoBean> contentInfoBeans
                    = bean.getTaskContentInfo();
            if (contentInfoBeans != null) {
                for (HistoryDetailBean.TaskContentInfoBean contentInfoBean : contentInfoBeans) {
                    //  Lv_1 lv_1;
                    entityList.add(new Lv_1(contentInfoBean.getTaskPosition()));
//                    List<MultiItemEntity> itemEntities = new ArrayList<>();
//                    lv_1.setSubItems(itemEntities);
                    List<HistoryDetailBean.TaskContentInfoBean.RepairFaultWorkBeanX> repairList =
                            contentInfoBean.getRepairFaultWork();
                    List<HistoryDetailBean.TaskContentInfoBean.MaintenanceWorkBean> maintenanceList =
                            contentInfoBean.getMaintenanceWork();
                    if (repairList != null && !repairList.isEmpty()) {
                        entityList.add(new Lv_2("报修作业", contentInfoBean
                                .getRepairStartData() + "-" + contentInfoBean.getRepairEndData()));
                        for (HistoryDetailBean.TaskContentInfoBean.RepairFaultWorkBeanX
                                repairFaultWorkBeanX : repairList) {
                            entityList.add(new Lv_2_1(repairFaultWorkBeanX.getFaultName(),
                                    repairFaultWorkBeanX.getDiagnosticName()));
                            List<HistoryDetailBean.TaskContentInfoBean.RepairFaultWorkBeanX.JobSchemeGroupBeanX>
                                    jobSchemeGroupBeanXList = repairFaultWorkBeanX.getJobSchemeGroup();
                            if (jobSchemeGroupBeanXList != null && !jobSchemeGroupBeanXList.isEmpty()) {
                                for (HistoryDetailBean.TaskContentInfoBean.RepairFaultWorkBeanX.JobSchemeGroupBeanX
                                        jobSchemeGroupBeanX : jobSchemeGroupBeanXList) {
                                    int associationStatus = jobSchemeGroupBeanX.getAssociationStatus();
                                    entityList.add(new Lv_2_1_1(
                                            (associationStatus == 0 ? "作业方案:" : "关联方案:") + jobSchemeGroupBeanX.getSchemeInfo(),
                                            "作业人:" + jobSchemeGroupBeanX.getSchemeUser(), associationStatus));
                                }
                            }
                        }
                    }
                    if (maintenanceList != null && !maintenanceList.isEmpty()) {
                        entityList.add(new Lv_3("保养作业", contentInfoBean
                                .getMaintenanceStartData() + "-" + contentInfoBean.getMaintenanceEndData()));
                        for (HistoryDetailBean.TaskContentInfoBean.MaintenanceWorkBean
                                maintenanceWorkBean : maintenanceList) {
                            String reasonOfNo = maintenanceWorkBean.getReasonOfNo();
                            String user = maintenanceWorkBean.getMaintenanceUser();
                            user = (reasonOfNo == null || reasonOfNo.isEmpty()) ?
                                    "作业人:" + user : ("未作业原因:" + reasonOfNo + "\n" +
                                    "作业人:" + user);
                            entityList.add(new Lv_3_1(maintenanceWorkBean.getMaintenanceWorkItems(),
                                    user));
                            List<HistoryDetailBean.TaskContentInfoBean.
                                    MaintenanceWorkBean.RepairFaultWorkBean> repairFaultWorkBeanList =
                                    maintenanceWorkBean.getRepairFaultWork();
                            if (repairFaultWorkBeanList != null && !repairFaultWorkBeanList.isEmpty()) {
                                StringBuilder str = new StringBuilder();
                                Lv_3_2 lv_3_2 = new Lv_3_2(str.toString());
                                entityList.add(lv_3_2);
                                for (HistoryDetailBean.TaskContentInfoBean.
                                        MaintenanceWorkBean.RepairFaultWorkBean
                                        repairFaultWorkBean : repairFaultWorkBeanList) {
                                    str.append(repairFaultWorkBean.getDiagnosticName()).append("/");
                                    List<HistoryDetailBean.TaskContentInfoBean
                                            .MaintenanceWorkBean.RepairFaultWorkBean
                                            .JobSchemeGroupBean> jobSchemeGroupBeans =
                                            repairFaultWorkBean.getJobSchemeGroup();
                                    if (jobSchemeGroupBeans != null && !jobSchemeGroupBeans.isEmpty()) {
                                        for (HistoryDetailBean.TaskContentInfoBean
                                                .MaintenanceWorkBean.RepairFaultWorkBean
                                                .JobSchemeGroupBean jobSchemeGroupBean : jobSchemeGroupBeans) {
                                            entityList.add(new Lv_2_1_1(
                                                    "作业方案:" + jobSchemeGroupBean.getSchemeInfo(),
                                                    "作业人:" + jobSchemeGroupBean.getSchemeUser(), 0));
                                        }
                                    }
                                }
                                lv_3_2.setDiagnose("诊断  " + str.substring(0, str.length() - 1));
                            }
                        }
                    }
                    List<HistoryDetailBean.TaskContentInfoBean.CheckedWorkBean> checkedWorkBeans =
                            contentInfoBean.getCheckedWork();
                    if (checkedWorkBeans != null && !checkedWorkBeans.isEmpty()) {
                        for (HistoryDetailBean.TaskContentInfoBean.CheckedWorkBean checkedWorkBean :
                                checkedWorkBeans) {
                            entityList.add(new Lv_4("过检作业", checkedWorkBean.getCheckedStartData()
                                    + "-" + checkedWorkBean.getCheckedEndData(),
                                    "过检结论：" + checkedWorkBean.getCheckedResult(),
                                    "过检人：" + checkedWorkBean.getCheckedUser()));
                        }
                    }
                }
            }
            HistoryDetailBean.CompletedGroupBean completedGroupBean = bean.getCompletedGroup();
            if (completedGroupBean != null) {
                List<HistoryDetailBean.CompletedGroupBean.CompletedStandardOperaBean>
                        completedStandardOperaBeans = completedGroupBean.getCompletedStandardOpera();
                if (completedStandardOperaBeans != null && !completedStandardOperaBeans.isEmpty()) {
                    entityList.add(new Lv_5("竣检标准"));
                    for (HistoryDetailBean.CompletedGroupBean.CompletedStandardOperaBean
                            completedStandardOperaBean : completedStandardOperaBeans) {
                        entityList.add(new Lv_5_1(completedStandardOperaBean.getTaskPosition(),
                                completedStandardOperaBean.getCompletedPro()));
                    }

                }
                List<HistoryDetailBean.CompletedGroupBean.CompletedWorkBean> completedWorkBeans
                        = completedGroupBean.getCompletedWork();
                if (completedWorkBeans != null && !completedWorkBeans.isEmpty()) {
                    for (HistoryDetailBean.CompletedGroupBean.CompletedWorkBean completedWorkBean :
                            completedWorkBeans) {
                        entityList.add(new Lv_4("竣检作业", completedWorkBean.getCompletedStartData()
                                + "-" + completedWorkBean.getCompletedEndData()
                                , completedWorkBean.getCompletedResult(), completedWorkBean.getCompletedUser()));
                    }
                }
            }

        }

        return entityList;
    }


}
